package org.resort.controller;

import com.sun.org.apache.xpath.internal.SourceTree;
import org.omg.CORBA.OBJECT_NOT_EXIST;
import org.resort.dao.RoomOrderDao;
import org.resort.entity.*;
import org.resort.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 郭文虎
 * <p>
 * 客房模块
 */
@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    RoomService roomService;
    @Autowired
    RoomOrderDao roomOrderDao;

    /**
     * 打开房间管理页面
     *
     * @return
     */
    @RequestMapping(value = "/openRoomManage.html", method = RequestMethod.GET)
    public String openRoomManage() {
        return "room/show";
    }

    /**
     * 获取所有房间订单信息
     *
     * @return
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getAllGet() {
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<RoomOrder> roomList = roomService.getAll();
        for (RoomOrder o : roomList) {
            o.show();
        }
        map.put("rows", roomList);
        map.put("total", roomList.size());
        return map;
    }

    /**
     * 打开添加房间信息页面
     */
    @RequestMapping("/openAddRoomorder")
    public String openAddRoomorder() {
        return "room/roomorderManageAdd";
    }

    /**
     * 添加一条房间管理信息
     */
    @RequestMapping("/addRoom")
    @ResponseBody
    public Map<String, Object> addRoomOrder(String startdate, String enddate, int memberid, int employeeid, int stateid, Double day) {
        System.out.println("startdate:" + startdate);
        System.out.println("enddate:" + enddate);
        System.out.println("memberid:" + memberid);
        System.out.println("employeeid:" + employeeid);
        System.out.println("stateid:" + stateid);
        System.out.println("day:" + day);
        //日期格式转换
        Date startdate1 = null;
        Date enddate1 = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            startdate1 = formatter.parse(startdate);
            enddate1 = formatter.parse(enddate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //添加
        Map<String, Object> map = new HashMap<String, Object>();
        int result = roomService.addRoomOrder(startdate1, enddate1, memberid, employeeid, stateid, day);
        System.out.println("result:" + result);
        map.put("result", result);
        return map;
    }

    /**
     * 打开退房管理信息页面
     *
     * @return
     */
    @RequestMapping("/openExitRoom")
    public String openExitRoom() {
        return "room/exitRoom";
    }

    @RequestMapping("/getAllExit")
    @ResponseBody
    public Map<String, Object> getAllExit() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<ExitRoom> exitRoomList = roomService.getAllExits();
        map.put("rows", exitRoomList);
        return map;
    }

    /**
     * 删除房间订单信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteRoomOrder")
    @ResponseBody
    public Map<String, Object> deleteRoomOrder(@RequestParam("id") int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        roomOrderDao.delOneEntityById(id);
        map.put("result", true);
        return map;
    }

    /**
     * 打开修改信息模态框页面
     */
    @RequestMapping(value = "/getRoomOrderById", method = RequestMethod.POST)
    public String getRoomOrderById(@RequestParam("id") int id, Map<String, Object> map) {
        map.put("roomorder", roomService.getRoomOrderById(id));
        return "room/roomorderManageUpdate";
    }

    /**
     * 修改退房记录信息
     *
     * @param id
     * @param startdate
     * @param enddate
     * @param memberid
     * @param employeeid
     * @param stateid
     * @param day
     * @return
     */
    @RequestMapping("/getupdateRoomOrder")
    @ResponseBody
    public Map<String, Object> getupdateRoomOrder(int id, Date startdate, Date enddate, int memberid, int employeeid, int stateid, Double day) {
        Map<String, Object> map = new HashMap<String, Object>();
        int updates = roomService.updateRoomOrder(id, startdate, enddate, memberid, employeeid, stateid, day);
        map.put("updates", updates);
        return map;
    }

    /**
     * 打开退房信息添加页面
     *
     * @return
     */
    @RequestMapping("/openAddExitRoom")
    public String openAddExitRoom(Map<String, Object> map) {
        map.put("dustmanList", roomService.getAllDustman());
        return "/room/exitRoomAdd";
    }

    @RequestMapping("/getPartRoomOrderByState")
    @ResponseBody
    public Map<String, Object> getPartRoomOrderByStateAndDustman(String stateName) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<RoomOrder> roomOrderList = roomService.getPartOrderByState(stateName);
        map.put("roomOrderList", roomOrderList);
        return map;
    }

    /**
     * 添加退房信息
     *
     * @param roomorderid
     * @param date
     * @param dustmanid
     * @return
     */
    @RequestMapping("/getAddExit")
    @ResponseBody
    public Map<String, Object> getAddExit(int roomorderid, String date, int dustmanid) {
        //日期格式转换
        Date date1 = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date1 = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int result = roomService.AddExitRoom(roomorderid, date1, dustmanid);
        System.out.println("roomorderid" + roomorderid);
        System.out.println("date" + date);
        System.out.println("dustmanid" + dustmanid);
        map.put("result", result);
        return map;
    }

    /**
     * 打开房间续费的页面
     */
    @RequestMapping("/openAppendRoomManage.html")
    public String openAppendRoomManage() {
        return "/room/appendRoom";
    }

    /**
     * 查看房间续费的信息
     *
     * @return
     */
    @RequestMapping("/getAppendRoom")
    @ResponseBody
    public Map<String, Object> getAppendRoom() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<AppendRoom> list = roomService.getAllAppendRoom();
        map.put("rows", list);
        return map;
    }

    /**
     * 打开客房类型信息页面入口
     *
     * @return
     */
    @RequestMapping("/openRoomType")
    public String openRoomType() {
        return "/room/roomtype";
    }

    @RequestMapping("/getAllRoomType")
    @ResponseBody
    public Map<String, Object> getAllRoomType() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<RoomType> list = roomService.getAllType();
        map.put("rows", list);
        return map;
    }

    /**
     * 打开添加房间续费页面
     *
     * @return
     */
    @RequestMapping("/openAppendRoomAdd")
    public String openAppendRoomAdd() {
        return "/room/appendRoomAdd";
    }

    @RequestMapping("/AddAppendRoom")
    @ResponseBody
    public Map<String, Object> AddAppendRoom(int roomorderid, String appenddate) {

        System.out.println("roomorderid" + roomorderid);
        System.out.println("appenddate" + appenddate);

        //日期格式转换
        Date date1 = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date1 = formatter.parse(appenddate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int result = roomService.getAddAppentRoom(roomorderid, date1);
        map.put("result", result);

        return map;
    }

    /**
     * 打开客房管理页面
     * 进行增删改查操作
     *
     * @return
     */
    @RequestMapping("/openRoomJsp")
    public String openRoomJsp() {
        return "/room/room";
    }

    /**
     * 获取所有客房管理信息
     *
     * @return
     */
    @RequestMapping("/getAllRoom")
    @ResponseBody
    public Map<String, Object> getAllRoom() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<Room> list = roomService.getRoom();
        map.put("rows", list);
        return map;
    }

    /**
     * 打开订单明细页面
     *
     * @return
     */
    @RequestMapping("/openRoomOrderInfoJsp")
    public String openRoomOrderInfoJsp() {
        return "/room/roomorderinfo";
    }

    @RequestMapping("/getAllRoomOrderInfo")
    @ResponseBody
    public Map<String, Object> getAllRoomOrderInfo() {
        Map<String, Object> map = new HashMap<String, Object>();
        List<RoomOrderInfo> list = roomService.getRoomOrderInfo();
        map.put("rows", list);
        return map;
    }

    /**
     * 打开添加客房类型模态框
     *
     * @return
     */
    @RequestMapping("/openAddRoomTypeJsp")
    public String openAddRoomTypeJsp() {
        return "/room/roomtypeAdd";
    }

    @RequestMapping("AddRoomType")
    @ResponseBody
    public Map<String, Object> AddRoomType(String name, String descriptor) {
        Map<String, Object> map = new HashMap<String, Object>();
        int result = roomService.AddRoomType(name, descriptor);
        map.put("result", result);
        return map;
    }

    /**
     * 打开添加客房管理页面
     *
     * @return
     */
    @RequestMapping("/openRoom")
    public String openRoom(/*Map<String,Object> map*/) {
        /*map.put("roomTypeList",roomService.getAllRoomType());*/
        return "/room/roomAdd";
    }

    @RequestMapping("/AddRoom")
    @ResponseBody
    public Map<String, Object> AddRoom(String name, int typeid, int stateid) {
        System.out.println("name" + name);
        System.out.println("typeid" + typeid);
        System.out.println("stateid" + stateid);
        Map<String, Object> map = new HashMap<String, Object>();
        int result = roomService.addRoom(name, typeid, stateid);
        map.put("result", result);
        return map;
    }

    /**
     * 打开添加订单明细模态框
     *
     * @return
     */
    @RequestMapping("openRoomOrderInfo")
    public String openRoomOrderInfo() {
        return "/room/roomorderInfoAdd";
    }

    /**
     * 添加一条订单明细
     *
     * @param roomid
     * @param roomorderid
     * @return
     */
    @RequestMapping("/AddRoomOrderInfo")
    @ResponseBody
    public Map<String, Object> AddRoomOrderInfo(int roomid, int roomorderid) {
        Map<String, Object> map = new HashMap<String, Object>();
        int result = roomService.addroomorderinfo(roomid, roomorderid);
        map.put("result", result);
        return map;
    }

    /**
     * 打开修改退房信息模态框
     *
     * @return
     */
    @RequestMapping(value = "/openUpdateExitRoom", method = RequestMethod.POST)
    public String openUpdateExitRoom(@RequestParam("id") int id, Map<String, Object> map) {
        map.put("exitroom", roomService.getExitRoomById(id));
        return "room/exitRoomUpdate";
    }

    /**
     * 修改退房记录信息
     *
     * @param id
     * @param roomorderid
     * @param date
     * @param dustmanid
     * @return
     */
    @RequestMapping("/updateExitRoom")
    @ResponseBody
    public Map<String, Object> updateExitRoom(int id, int roomorderid, String date, int dustmanid) {
        System.out.println("id" + id);
        System.out.println("roomorderid" + roomorderid);
        System.out.println("date" + date);
        System.out.println("dustmanid" + dustmanid);
        //日期格式转换
        Date date1 = null;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date1 = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Map<String, Object> map = new HashMap<String, Object>();
        int updates = roomService.updateExitRoom(id, roomorderid, date1, dustmanid);
        map.put("updates", updates);
        return map;
    }

    /**
     * 打开修改续费信息的模态框
     *
     * @return
     */
    @RequestMapping(value = "/openappendroomdel", method = RequestMethod.POST)
    public String openappendroomdel(@RequestParam("id") int id, Map<String, Object> map) {
        map.put("appendroom", roomService.getAppendRoomByid(id));
        return "/room/appendRoomUpdate";
    }

    /**
     * 修改续费信息
     *
     * @param id
     * @param roomorderid
     * @param appenddate
     * @return
     */
    @RequestMapping("/updateAppendRoom")
    @ResponseBody
    public Map<String, Object> updateAppendRoom(int id, int roomorderid, Date appenddate) {
        Map<String, Object> map = new HashMap<String, Object>();
        int updates = roomService.updateAppendRoom(id, roomorderid, appenddate);
        map.put("updates", updates);
        return map;
    }
}
